package org.zw.android.framework.app;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;

/**
 * 
 * @author zhouwei
 *
 */
public final class AppBundleUtil {
	
	private Intent intent ;
	
	private AppBundleUtil(Intent intent){
		this.intent	= intent ;
	}
	
	public static AppBundleUtil attach(Intent intent){
		return new AppBundleUtil(intent);
	}
	
	private Bundle getBundle(){
		return intent != null ? intent.getExtras() : null ;
	}
	
	public final int findInteger(String key){
		Bundle bundle = getBundle() ;
		return bundle != null ? bundle.getInt(key) : -1;
	}
	
	public final long findLong(String key){
		Bundle bundle = getBundle() ;
		return bundle != null ? bundle.getLong(key) : -1;
	}
	
	public final boolean findBoolean(String key){
		Bundle bundle = getBundle() ;
		return bundle != null ? bundle.getBoolean(key) : false;
	}
	
	public final String findString(String key){
		Bundle bundle = getBundle() ;
		return bundle != null ? bundle.getString(key) : null;
	}
	
	@SuppressWarnings("unchecked")
	public final <T> T findObject(String key){
		
		try{
			Bundle bundle = getBundle() ;
			return bundle != null ? (T) bundle.getSerializable(key) : null ;
		} catch(Exception e){
			e.printStackTrace() ;
		}
		
		return null ;
	}
	
	public final ArrayList<String> findArrayList(String key){
		
		try{
			Bundle bundle = getBundle() ;
			return bundle != null ? bundle.getStringArrayList(key) : null;
		} catch(Exception e){
			e.printStackTrace() ;
		}
		
		return null ;
	}
}
